package com.greenet.companytemplate.manager.web;

import com.greenet.companytemplate.manager.utils.DirectoryToZip;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by Administrator on 2016/5/4.
 */
@Controller
public class DownloadController {
    protected Logger logger= LoggerFactory.getLogger(getClass());
    @Autowired
    private DirectoryToZip directoryToZip;

    @RequestMapping("/download")
    public void download(@RequestParam("url") String url,
                         @RequestParam("filename") String filename,
                         HttpServletRequest request,HttpServletResponse response) throws IOException {
        String endname=filename.substring(0, filename.lastIndexOf("."));
        response.reset();
        response.setContentType("application/x-download");
        response.addHeader("Content-Disposition", "attachment;filename=download.zip");
        byte[] b = new byte[10240];
        String path=request.getSession().getServletContext().getRealPath("/resources/upload/");
        OutputStream outputStream = response.getOutputStream();
        directoryToZip.toZip(path+url,outputStream);
    }
}
